package com.dianrui.moonfire.activity;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.util.EventBusConstants;
import com.dianrui.moonfire.util.NoDoubleClickUtils;
import com.google.gson.JsonObject;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:押金退款失败
 * User: luoziyin
 * Date: 2020-8-17
 */
public class WithDrawFailedSubmitActivity extends BaseActivity {
    private  String financeId;
    private String name,feedback;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.warning_txt)
    TextView warningTxt;
    @BindView(R.id.warning_txt1)
    TextView warningTxt1;
    @BindView(R.id.zfb_real_name)
    EditText zfbRealName;
    @BindView(R.id.zfb_account)
    EditText zfbAccount;


    @Override
    public int getContentViewId() {
        return R.layout.activity_withdraw_submit;
    }

    @Override
    public void initView() {
        if (getIntent() != null && getIntent().getExtras()!=null) {
            financeId=getIntent().getStringExtra("financeId");
            name=getIntent().getStringExtra("name");
            feedback=getIntent().getStringExtra("feedback");
            zfbRealName.setText(name);
            zfbAccount.setText(feedback);
        }
        customInit(true);
        title.setText(getString(R.string.withdraw_failed_title));
        SpannableString spannableString1 = new SpannableString(getString(R.string.withdraw_failed_top_txt));
        spannableString1.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        warningTxt1.setText(spannableString1);

        SpannableString spannableString = new SpannableString(getString(R.string.withdraw_failed_txt));
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        warningTxt.setText(spannableString);

    }

    @Override
    public void initData() {}


    @OnClick({R.id.back, R.id.refund_faild_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.refund_faild_btn:
                if (StringUtils.isEmpty(zfbRealName.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.withdraw_failed_name_input));
                } else if (StringUtils.isEmpty(zfbAccount.getText().toString())) {
                    ToastUtil.showToast(getString(R.string.withdraw_failed_account_input));
                } else {
                    Refund();
                }
                break;

            default:
                break;
        }
    }

    private void Refund() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        if (!StringUtils.isEmpty(financeId)) {
            jsonObject.addProperty("finance_id", financeId);
        }
        jsonObject.addProperty("name", zfbRealName.getText().toString().trim());
        jsonObject.addProperty("feedback", zfbAccount.getText().toString().trim());
        XHttpRequest.getInstance().postRequests(Url.REFUNDFAILED, jsonObject.toString(), new XHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                EventBus.getDefault().post(EventBusConstants.REFRESH_WALLET_LIST);
                                ToastUtil.showToast(jsonObject.optString("message"));
                                finish();
                            } else {
                                ToastUtil.showToast(jsonObject.optString("message"));
                            }

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));

            }
        });


    }


}
